package com.tous.tool.service;

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author mengwei
 * @description HlsConversionService
 * @createDate 2025/7/21 11:39
 */
@Service
public class HlsConversionService {

    @Value("${video.hls.output-dir:hls_output}")
    private String outputDir;

    public void convertToHls(File inputFile, String outputName) throws IOException {
        String outputPath = outputDir + File.separator + outputName;
        new File(outputPath).mkdirs();

        // 创建多个分辨率的HLS流
        createVariant(outputPath, inputFile, "low", 426, 240, 400_000);
        createVariant(outputPath, inputFile, "medium", 640, 360, 800_000);
        createVariant(outputPath, inputFile, "high", 854, 480, 1200_000);
        createVariant(outputPath, inputFile, "hd", 1280, 720, 2500_000);

        // 生成主m3u8文件
        generateMasterPlaylist(outputPath, outputName);
    }

    private void createVariant(String outputPath, File inputFile,
                               String quality, int width, int height, int bitrate) throws IOException {
        String variantPath = outputPath + File.separator + quality;
        new File(variantPath).mkdirs();

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);
        grabber.start();

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
                variantPath + File.separator + "playlist.m3u8",
                width, height
        );

        recorder.setFormat("hls");
        recorder.setOption("hls_time", "10");
        recorder.setOption("hls_list_size", "0");
        recorder.setOption("hls_segment_filename", variantPath + File.separator + "segment_%03d.ts");
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setVideoBitrate(bitrate);
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.start();

        // 转码处理...
        // ...省略帧处理代码

        grabber.close();
        recorder.close();
    }

    private void generateMasterPlaylist(String outputPath, String outputName) throws IOException {
        String masterContent = "#EXTM3U\n" +
                "#EXT-X-VERSION:3\n" +
                "#EXT-X-STREAM-INF:BANDWIDTH=400000,RESOLUTION=426x240\n" +
                "low/playlist.m3u8\n" +
                "#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360\n" +
                "medium/playlist.m3u8\n" +
                "#EXT-X-STREAM-INF:BANDWIDTH=1200000,RESOLUTION=854x480\n" +
                "high/playlist.m3u8\n" +
                "#EXT-X-STREAM-INF:BANDWIDTH=2500000,RESOLUTION=1280x720\n" +
                "hd/playlist.m3u8";

        Files.write(Paths.get(outputPath, "playlist.m3u8"), masterContent.getBytes());
    }



}
